layui.config({
	base: '/public/admini/js/module/'
}).extend({
	dialog: 'dialog',
});
/**
 * 后台JS主入口
 */
layui.use(['form', 'jquery',  'layer', 'laypage', 'dialog','upload', 'element', 'laydate'], function() {
	var form = layui.form(),
		layer = layui.layer,
		$ = layui.jquery,
		element = layui.element,
		laydate = layui.laydate,
		dialog = layui.dialog;
	
	/**
	 * AJAX全局设置
	 */
	$.ajaxSetup({
		type: "post",
		dataType: "json"
	});
	
	/**
	 * 通用日期时间选择
	 */
//	laydate.render({
//		elem: '#datetime', //指定元素
//		type: 'datetime'
//	});

	layui.upload({
		url: '/admini/index/upload',
		success: function(res) {
			document.getElementById("images").value = res.data.src;
			document.getElementById("imagessrc").src = res.data.src;
		}
	});
	/* 监听状态设置开关 */
	form.on('switch(switchStatus)', function(data) {
		var that = $(this),
			status = 0;
		if(!that.attr('data-href')) {
			layer.msg('请设置data-href参数');
			return false;
		}
		if(this.checked) {
			status = 1;
		}
		$.get(that.attr('data-href'), {
			val: status
		}, function(res) {
			layer.msg(res.msg);
			if(res.code == 0) {
				that.trigger('click');
				form.render('checkbox');
			}
		});
	});

    
	//获取当前iframe的name值
	var iframeObj = $(window.frameElement).attr('name');
	//全选
	form.on('checkbox(allChoose)', function(data) {
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
		child.each(function(index, item) {
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});
	//渲染表单
	form.render();	
	//顶部添加
	$('.addBtn').click(function() {
		var url=$(this).attr('data-url');
		//将iframeObj传递给父级窗口,执行操作完成刷新
		parent.page("菜单添加", url, iframeObj, w = "700px", h = "620px");
		return false;

	}).mouseenter(function() {

		dialog.tips('添加', '.addBtn');

	})
	
	/**
	 * 通用表单提交(AJAX方式)
	 */
	form.on('submit(login)', function(data) {
		$.ajax({
			url: data.form.action,
			type: data.form.method,
			data: data.field,
			 dataType: "json",  
			success: function(info) {
				if(info.code === 1) {
					setTimeout(function() {
						location.href = info.url;
					}, 1000);
				}
				layer.msg(info.message);
			}
		});
		return false;
	});
	/**
	 * 通用Ajax
	 */
	$('.ajax-do').on('click', function() {
		var _href = $(this).attr('href');
		layer.open({
			shade: false,
			content: '确定操作？',
			btn: ['确定', '取消'],
			yes: function(index) {
				$.ajax({
					url: _href,
					type: "get",
					success: function(info) {
						if(info.code === 1) {
							setTimeout(function() {
								location.href = info.url;
							}, 1000);
						}
						layer.msg(info.msg);
					}
				});
				layer.close(index);
			}
		});
		return false;
	});
	/**
	 * 通用列表组
	 */
	$('.page-btns').click(function() {
		var that = $(this),
			code = function(that) {
				var href = that.attr('href') ? that.attr('href') : that.attr('data-href');
				if(!href) {
					layer.msg('请设置data-href参数');
					return false;
				}
				if($('.checkbox-ids:checked').length <= 0) {
					layer.msg('请选择要操作的数据');
					return false;
				}
				var query = $('#listForm').serialize();
				$.post(href, query, function(res) {
					layer.msg(res.msg, {
						time: 1000
					}, function() {
						if(res.code != 0) {
							location.reload();
						}
					});
				});
			};
		if(that.hasClass('confirm')) {
			var tips = that.prop('title') ? that.prop('title') : '您确定要操作吗？';
			layer.confirm(tips, {
				title: false,
				closeBtn: 0
			}, function(index) {
				code(that);
				layer.close(index);
			});
		} else {
			code(that);
		}
		return false;
	});
	/**
	 * 更新排序
	 * */
	$('.input-sort').focusout(function() {
		var that = $(this),
			_val = that.val();
		if(_val == '') return false;
		if(that.attr('data-value') == _val) return false;
		if(!that.attr('data-href')) {
			layer.msg('请设置data-href参数');
			return false;
		}
		$.post(that.attr('data-href'), {
			val: _val
		}, function(res) {
			if(res.code == 1) {
				that.attr('data-value', _val);
			}
			layer.msg(res.msg);
		});
	});
	//顶部批量删除
	$('.delBtn').click(function() {
		var url=$(this).attr('data-url');
		dialog.confirm({
			message:'您确定要删除选中项',
			success:function(){
				
				layer.msg(url)
			},
			cancel:function(){
				layer.msg('取消了')
			}
		})
		return false;

	}).mouseenter(function() {

		dialog.tips('批量删除', '.delBtn');

	})	
	//列表添加
	$('#table-list').on('click', '.add-btn', function() {
		var url=$(this).attr('data-url');
		//将iframeObj传递给父级窗口
		parent.page("菜单添加", url, iframeObj, w = "700px", h = "620px");
		return false;
	})
	//列表删除
	$('#table-list').on('click', '.del-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		dialog.confirm({
			message:'您确定要进行删除吗？',
			success:function(){
				layer.msg('确定了')
			},
			cancel:function(){
				layer.msg('取消了')
			}
		})
		return false;
	})
	//列表跳转
	$('#table-list,.tool-btn').on('click', '.go-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		window.location.href=url+"?id="+id;
		return false;
	})
	//编辑栏目
	$('#table-list').on('click', '.edit-btn', function() {
		var That = $(this);
		var id = That.attr('data-id');
		var url=That.attr('data-url');
		//将iframeObj传递给父级窗口
		parent.page("菜单编辑", url + "?id=" + id, iframeObj, w = "700px", h = "620px");
		return false;
	})
});

/**
 * 控制iframe窗口的刷新操作
 */
var iframeObjName;

//父级弹出页面
function page(title, url, obj, w, h) {
	if(title == null || title == '') {
		title = false;
	};
	if(url == null || url == '') {
		url = "404.html";
	};
	if(w == null || w == '') {
		w = '700px';
	};
	if(h == null || h == '') {
		h = '350px';
	};
	iframeObjName = obj;
	//如果手机端，全屏显示
	if(window.innerWidth <= 768) {
		var index = layer.open({
			type: 2,
			title: title,
			area: [320, h],
			fixed: false, //不固定
			content: url
		});
		layer.full(index);
	} else {
		var index = layer.open({
			type: 2,
			title: title,
			area: [w, h],
			fixed: false, //不固定
			content: url
		});
	}
}

/**
 * 刷新子页,关闭弹窗
 */
function refresh() {
	//根据传递的name值，获取子iframe窗口，执行刷新
	if(window.frames[iframeObjName]) {
		window.frames[iframeObjName].location.reload();

	} else {
		window.location.reload();
	}

	layer.closeAll();
}
//加载省数据
    function loadProvince() {
        var proHtml = '';
        for (var i = 0; i < areaData.length; i++) {
            proHtml += '<option value="' + areaData[i].provinceName + '_' + areaData[i].mallCityList.length + '_' + i + '">' + areaData[i].provinceName + '</option>';
        }
        //初始化省数据
        $form.find('select[name=province]').append(proHtml);
        form.render();
        form.on('select(province)', function(data) {
            $form.find('select[name=area]').html('<option value="">请选择县/区</option>').parent().hide();
            var value = data.value;
            var d = value.split('_');
            var code = d[0];
            var count = d[1];
            var index = d[2];
            if (count > 0) {
                loadCity(areaData[index].mallCityList);
            } else {
                $form.find('select[name=city]').parent().hide();
            }
        });
    }
     //加载市数据
    function loadCity(citys) {
        var cityHtml = '';
        for (var i = 0; i < citys.length; i++) {
            cityHtml += '<option value="' + citys[i].cityName + '_' + citys[i].mallAreaList.length + '_' + i + '">' + citys[i].cityName + '</option>';
        }
        $form.find('select[name=city]').html(cityHtml).parent().show();
        form.render();
        form.on('select(city)', function(data) {
            var value = data.value;
            var d = value.split('_');
            var code = d[0];
            var count = d[1];
            var index = d[2];
            if (count > 0) {
                loadArea(citys[index].mallAreaList);
            } else {
                $form.find('select[name=area]').parent().hide();
            }
        });
    }
     //加载县/区数据
    function loadArea(areas) {
        var areaHtml = '';
        for (var i = 0; i < areas.length; i++) {
            areaHtml += '<option value="' + areas[i].areaName + '">' + areas[i].areaName + '</option>';
        }
        $form.find('select[name=area]').html(areaHtml).parent().show();
        form.render();
        form.on('select(area)', function(data) {
            //console.log(data);
        });
    }
   
